package name.schooley.umarks.domain;

import java.lang.String;
import java.lang.SuppressWarnings;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import name.schooley.umarks.domain.Bookmark;

privileged aspect Tag_Roo_Finder {
    
    @SuppressWarnings("unchecked")
    public static Query Tag.findTagsByBookmarks(Set<Bookmark> bookmarks) {
        if (bookmarks == null) throw new IllegalArgumentException("The bookmarks argument is required");
        EntityManager em = Tag.entityManager();
        StringBuilder queryBuilder = new StringBuilder("SELECT Tag FROM Tag AS tag WHERE");
        for (int i = 0; i < bookmarks.size(); i++) {
            if (i > 0) queryBuilder.append(" AND");
            queryBuilder.append(" :bookmarks_item").append(i).append(" MEMBER OF tag.bookmarks");
        }
        Query q = em.createQuery(queryBuilder.toString());
        int bookmarksIndex = 0;
        for (Bookmark _bookmark: bookmarks) {
            q.setParameter("bookmarks_item" + bookmarksIndex++, _bookmark);
        }
        return q;
    }
    
    @SuppressWarnings("unchecked")
    public static Query Tag.findTagsByTag(String tag) {
        if (tag == null || tag.length() == 0) throw new IllegalArgumentException("The tag argument is required");
        EntityManager em = Tag.entityManager();
        Query q = em.createQuery("SELECT Tag FROM Tag AS tag WHERE tag.tag = :tag");
        q.setParameter("tag", tag);
        return q;
    }
    
}
